/*
espui_core.js

The MIT License

Copyright (c) 2011 Yuya.Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// UIライブラリ

var esp = {};
esp.ui = {};
// ------------------------------------------------------------------
//! 共通定義
esp.ui.Mouse = {
	__proto__:{
		Button:{
			Left:0,
			Right:2,
			Middle:1
		},
		absX:0,
		absY:0,
		button:-1,
		update:function(event){
			this.absX = event.clientX + document.body.scrollLeft;
			this.absY = event.clientY + document.body.scrollTop;
			this.button = event.button;
		}
	}
};

// ------------------------------------------------------------------
//! カラーテーマ
esp.ui.Theme = {
	colorToString:function(col){return "#"+(col.R.toString(16)+col.G.toString(16)+col.B.toString(16));},
	addColor:function(col, add){return {R:((col.R+add)>255?255:(col.R+add)), G:((col.G+add)>255?255:(col.G+add)), B:((col.B+add)>255?255:(col.B+add))};},
	BGColor:{R:64,G:64,B:64},
	BorderColor:{R:16,G:16,B:16},
	TitleBarColor:{R:200,G:210,B:220}
};
esp.ui.Theme.Hover = {
};
esp.ui.Theme.Active = {
};

// ------------------------------------------------------------------
//! システム
esp.ui.System = function(){
	var _this = this;

	// マウスイベントフック用
	this.stdMouseEvent={
		downFunc:null,
		moveFunc:null,
		upFunc:null,
		changeFunc:null
	};
	// マウスイベントをフックするようにする
	document.onmousedown = function(event){_this.onMouseDown(event);};
	document.onmousemove = function(event){_this.onMouseMove(event);};
	document.onmouseup = function(event){_this.onMouseUp(event);};
}

//! スタンダードマウスイベントの設定
/*!
 * @param	downFunc	マウスがボタンが押されたときに呼び出される関数	func(event)
 * @param	moveFunc	マウスが移動されたときに呼び出される関数			func(event)
 * @param	upFunc		マウスがボタンが離されたときに呼び出される関数	func(event)
 * @param	changeFunc	このイベント関数が再度呼ばれたときに呼び出される関数	func()
 */
esp.ui.System.prototype.setStdMouseEvent = function(downFunc, moveFunc, upFunc, changeFunc){
	var tmpChangeFunc = this.stdMouseEvent.changeFunc;

	this.stdMouseEvent.downFunc = downFunc;
	this.stdMouseEvent.moveFunc = moveFunc;
	this.stdMouseEvent.upFunc = upFunc;
	this.stdMouseEvent.changeFunc = changeFunc;

	if(tmpChangeFunc){
		tmpChangeFunc();
	}
}
esp.ui.System.prototype.onMouseDown = function(event){
	esp.ui.Mouse.update(event);
	if(this.stdMouseEvent.donwFunc){
		this.stdMouseEvent.donwFunc(event);
	};
}
esp.ui.System.prototype.onMouseMove = function(event){
	esp.ui.Mouse.update(event);
	if(this.stdMouseEvent.moveFunc){
		this.stdMouseEvent.moveFunc(event);
	};
}
esp.ui.System.prototype.onMouseUp = function(event){
	esp.ui.Mouse.update(event);
	if(this.stdMouseEvent.upFunc){
		this.stdMouseEvent.upFunc(event);
	};
}
esp.ui.system = new esp.ui.System();
// ------------------------------------------------------------------
//! メニューバー
esp.ui.Menu = function(parent){
}
// ------------------------------------------------------------------
//! ウィンドウ
esp.ui.Window = function(parent){
	var _this = this;

	var titleBarTd = document.createElement("td");
	titleBarTd.root = this;
	titleBarTd.style.textAlign = "center";
	titleBarTd.style.width = "100%";
	titleBarTd.style.height = this.titleBarHeight;
	titleBarTd.style.fontSize = this.titleBarHeight;
	titleBarTd.style.cursor = "move";
	titleBarTd.style.margin = "0px";
	titleBarTd.style.padding = "0px";
	titleBarTd.style.borderWidth = "0 0 1px 0";
	titleBarTd.style.borderStyle = "solid";
	titleBarTd.style.borderColor = esp.ui.Theme.colorToString(esp.ui.Theme.BorderColor);
	titleBarTd.style.background = "-webkit-gradient(linear, left top, left bottom, from("+
		esp.ui.Theme.colorToString(esp.ui.Theme.addColor(esp.ui.Theme.TitleBarColor, 30))+"), to("+
		esp.ui.Theme.colorToString(esp.ui.Theme.TitleBarColor)+"))";
	// ドラッグイベント処理用
	titleBarTd.onmousedown = function(event){_this.onMouseDownTitleBar(event)};

	var titleBarTr = document.createElement("tr");
	var bodyTd = document.createElement("td");
	var bodyTr = document.createElement("tr");
	var iframe = document.createElement("iframe");
	iframe.style.width = "100%";
	iframe.style.height = "100%";
	iframe.style.borderWidth = "0 0 0 0";
	iframe.style.borderStyle = "solid";
	var frameTd = document.createElement("td");
	frameTd.style.margin = "0px";
	frameTd.style.padding = "0px";
	var frameTr = document.createElement("tr");
	frameTr.style.margin = "0px";
	frameTr.style.padding = "0px";
	var frameTable = document.createElement("table");
	frameTable.style.borderRadius = "5px 5px 5px 5px/ 5px 5px 5px 5px";
	frameTable.style.borderWidth = "1px";
	frameTable.style.borderStyle = "solid";
	frameTable.style.margin = "0px";
	frameTable.style.padding = "0px";
	frameTable.style.boxShadow = "2px 2px 3px #000";
	frameTable.style.position = "relative";
	frameTable.style.top = "0px";
	frameTable.style.left = "0px";

	titleBarTr.appendChild(titleBarTd);
	frameTd.appendChild(iframe);
	frameTr.appendChild(frameTd);
	frameTable.appendChild(titleBarTr);
	frameTable.appendChild(frameTr);
	
	parent.appendChild(frameTable);

	this.titleBarTd = titleBarTd;
	this.body = iframe.contentDocument.body;
	this.frameTable = frameTable;
	this.clickFromX = 0;
	this.clickFromY = 0;
	this.moveBaseX = 0;
	this.moveBaseY = 0;
}
esp.ui.Window.prototype.titleBarHeight = "10px";
//! windowの先頭要素を取得する
esp.ui.Window.prototype.getTopElement = function(){
	return this.frameTable;
}
//! windowの中のbody要素を取得する
esp.ui.Window.prototype.getBodyElement = function(){
	return this.body;
}

//! windowの中のbody要素の子に設定する
esp.ui.Window.prototype.appendChild = function(child){
	this.body.appendChild(child);
}

//! windowに名前を設定する
esp.ui.Window.prototype.setName = function(name){
	this.titleBarTd.innerText = name;
}
//! 横幅を設定する
esp.ui.Window.prototype.setWidth = function(width){
	this.frameTable.style.width = width + "px";
}
//! 横幅を設定する
esp.ui.Window.prototype.setHeight = function(height){
	this.frameTable.style.height = height + "px";
}
//! サイズをまとめて設定する
esp.ui.Window.prototype.setSize = function(width, height){
	this.setWidth(width);
	this.setHeight(height);
}
//! 横幅を取得する
esp.ui.Window.prototype.getWidth = function(){
	return 0;
}
//! 縦幅を取得する
esp.ui.Window.prototype.getHeight = function(){
	return 0;
}
//! 座標を設定する
esp.ui.Window.prototype.setPosition = function(x, y){
	this.frameTable.style.left = x+"px";
	this.frameTable.style.top = y+"px";
}
//! X座標を取得する
esp.ui.Window.prototype.getX = function(){
	return parseInt(this.frameTable.style.left.replace("px", ""));
}
//! Y座標を取得する
esp.ui.Window.prototype.getY = function(){
	return parseInt(this.frameTable.style.top.replace("px", ""));
}
//! タイトルバーマウス処理
esp.ui.Window.prototype.onMouseDownTitleBarStub = function(event){
	this.root.onMouseDownTitleBar(event);
}
esp.ui.Window.prototype.onMouseDragTitleBarStub = function(event){
	this.root.onMouseDragTitleBar(event);
}
esp.ui.Window.prototype.onMouseUpTitleBarStab = function(event){
	this.root.onMouseUpTitleBar(event);
}
esp.ui.Window.prototype.onMouseOutTitleBarStab = function(event){
	this.root.onMouseOutTitleBar(event);
}
esp.ui.Window.prototype.onMouseDownTitleBar = function(event){
	if(esp.ui.Mouse.button == esp.ui.Mouse.Button.Left){
		this.clickFromX = esp.ui.Mouse.absX;
		this.clickFromY = esp.ui.Mouse.absY;
		this.moveBaseX = this.getX();
		this.moveBaseY = this.getY();
		// マウスイベントをグローバルなイベントに登録する
		var _this = this;
		esp.ui.system.setStdMouseEvent(null, function(event){_this.onMouseDragTitleBar(event);}, function(event){_this.onMouseUpTitleBar(event);}, null);
	}
}
esp.ui.Window.prototype.onMouseDragTitleBar = function(event){
	if(esp.ui.Mouse.button == esp.ui.Mouse.Button.Left){
		var offX = esp.ui.Mouse.absX - this.clickFromX;
		var offY = esp.ui.Mouse.absY - this.clickFromY;
		this.setPosition(this.moveBaseX + offX, this.moveBaseY + offY);
	}
}
esp.ui.Window.prototype.onMouseUpTitleBar = function(event){
	this.clickFromX = 0;
	this.clickFromY = 0;
	this.moveBaseX = 0;
	this.moveBaseY = 0;
	esp.ui.system.setStdMouseEvent(null, null, null, null);
}

// ------------------------------------------------------------------


// ------------------------------------------------------------------
//! 初期化
esp.ui.Init = function(){
	// 背景色設定
	document.body.style.backgroundColor = esp.ui.Theme.colorToString(esp.ui.Theme.BGColor);
	document.body.style.background = "-webkit-gradient(linear, left top, left bottom, from("+
		esp.ui.Theme.colorToString(esp.ui.Theme.addColor(esp.ui.Theme.BGColor, 50))+"), to("+
		esp.ui.Theme.colorToString(esp.ui.Theme.BGColor)+"))";
}
esp.ui.Init();
// ------------------------------------------------------------------

// テストコード
var espwin = new esp.ui.Window(document.body);
espwin.setSize(200, 300);
espwin.setName("test window");



